<?php

namespace App\Morefun\Prize;

use App\Contracts\Services\Storage\StorageService;
use App\Entities\Prize;
use App\Entities\PrizeRecord;
use Carbon\Carbon;
use Illuminate\Container\Container;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Arr;
use Ramsey\Uuid\Uuid;

class PrizeService
{
    public function formatMultiple($activityId, array $prizes)
    {
        $formatted = [];
        $now = Carbon::now()->toDateTimeString();

        foreach ($prizes as $of => $prize) {
            $formatted[] = [
                'id' => Uuid::uuid4()->toString(),
                'activity_id' => $activityId,
                'alias_name' => Arr::get($prize, 'alias_name', ''),
                'prize_id' => Arr::get($prize, 'prize_id'),
                'surplus' => Arr::get($prize, 'availables', 0),
                'max_amount' => Arr::get($prize, 'max_amount', 0),
                'winning_limit' => Arr::get($prize, 'winning_limit'),
                'stock_limit' => Arr::get($prize, 'stock_limit'),
                'winning_probability' => Arr::get($prize,'winning_probability'),
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        return $formatted;
    }

    public function fillAvailableNumber(AbstractPaginator $prizes)
    {
        $storage = Container::getInstance()->make(StorageService::class);
        foreach ($prizes as $prize) {
            $records = isset($prize->records) ? $prize->records : [];
            $summaries = array_column($records->toArray(), 'summary', 'updated_at');
            ksort($summaries);
            $available = end($summaries);
            $prize->availables = $available + 0;
            $prize->background_image = $storage->toHttpUrl($prize->background_image);
            unset($prize->records);
        }

        return $prizes;
    }

    public function fillAvailableNumberForBindings(AbstractPaginator $bindings)
    {
        $prizeIds = [];

        foreach ($bindings as $binding) {
            $prizeIds[] = $binding->prize_id;
        }
        /*
        $prizes = Prize::with('records')
            ->whereIn('id', $prizeIds)
            ->get();

        $availables = [];

        foreach ($prizes as $prize) {
            $records = isset($prize->records) ? $prize->records : [];
            $summaries = array_column($records->toArray(), 'summary', 'updated_at');
            ksort($summaries);
            $availables[$prize->id] = end($summaries);
        }
        */
        $storage = Container::getInstance()->make(StorageService::class);

        foreach ($bindings as $binding) {
            $binding->prize->background_image = $storage->toHttpUrl($binding->prize->background_image);
            $records = isset($binding->prize->records) ? $binding->prize->records : [];
            $summaries = array_column($records->toArray(), 'summary', 'updated_at');
            ksort($summaries);
            $available = end($summaries);
            $binding->prize->availables = $available + 0;
            unset($binding->prize->records);
        }

        return $bindings;
    }
}
